﻿using Hhmocon.Mes.Application;
using Hhmocon.Mes.Application.Base;
using Hhmocon.Mes.Application.Energy;
using Hhmocon.Mes.Application.Response;
using Hhmocon.Mes.Repository.Domain;
using Hhmocon.Mes.Util;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using Hhmocon.Mes.Repository.Response;
using static Hhmocon.Mes.Repository.Response.GetGridElecdistrbutionAmountResponse;

namespace Hhmocon.Mes.WebApi.Controllers.Base
{
    [Route("api/[controller]/[action]")]
    [EnableCors(PolicyName = "_hhmoconmesAllowSpecificOrigins")]
    [ApiController]
    public class EnergyElecdistributionController : ControllerBase
    {
        private EnergyElecdistributionApp _app;
        private PikachuApp _pikachuApp;

        public EnergyElecdistributionController(EnergyElecdistributionApp app, PikachuApp picachuRepository)
        {
            _app = app;
            _pikachuApp = picachuRepository;
        }


        /// <summary>
        /// 配电网电量页面  得到电网列表数据 购电量(电网) 发电量(光伏) 充电量(储能) 放电量(储能)
        /// </summary>
        /// <param name="StartTime">开始时间 年月日</param>
        /// <param name="EndTime">结束时间 年月日</param>
        /// <param name="id">馈线线路id</param>
        /// <returns></returns>
        [HttpPost]
        public Response<GetGridElecdistributionResponse> GetGridElecdistribution(DateTime StartTime,DateTime EndTime,string id)
        {
            var result = new Response<GetGridElecdistributionResponse>();
            try
            {
                result.Result = _app.GetGridElecdistribution(StartTime,EndTime,id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 配电网电量页面  今日电量百分比
        /// </summary>
        /// <param name="StartTime">开始时间 年月日</param>
        /// <param name="EndTime">结束时间 年月日</param>
        /// <param name="id">馈线线路id</param>
        /// <returns></returns>
        [HttpPost]
        public Response<List<GetGridElecdistrbutionAmountBaifenbi>> GetGridElecdistributionBaifenBi(string id)
        {
            var result = new Response<List<GetGridElecdistrbutionAmountBaifenbi>>();
            try
            {
                result.Result = _app.GetGridElecdistributionAmountNowDay(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 配电网总电量页面  每日总表用电量 月度总表用电量 年度总表用电量
        /// </summary>
        /// <param name="StartTime">开始时间 年月日</param>
        /// <param name="EndTime">结束时间 年月日</param>
        /// <returns></returns>
        [HttpPost]
        public Response<GetGridElecdistrbutionAmountResponse> GetGridElecdistributionGeneral(DateTime StartTime, DateTime EndTime,string id)
        {
            var result = new Response<GetGridElecdistrbutionAmountResponse>();
            try
            {
                result.Result = _app.GetGridElecdistributionAmount(StartTime, EndTime,id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 总 电  折线图 月份
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="id">i</param>
        /// <returns></returns>
        [HttpPost]
        public Response<GridElecdistributionLineChartResponse> GetGridElecdistributionChartNoArgs(string id)
        {
            var result = new Response<GridElecdistributionLineChartResponse>();
            try
            {
                result.Result = _app.GetGridElecdistributionAmountLineChart(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 总 电  折线图  带时间参数 月份
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="id">i</param>
        /// <returns></returns>
        [HttpPost]
        public Response<GridElecdistributionLineChartResponse> GetGridElecdistributionChart(DateTime StartTime,DateTime EndTime, string id)
        {
            var result = new Response<GridElecdistributionLineChartResponse>();
            try
            {
                result.Result = _app.GetGridElecdistributionAmountLineChart(StartTime,EndTime,id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 总 电  折线图  年份
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="id">i</param>
        /// <returns></returns>
        [HttpPost]
        public Response<GridElecdistributionLineChartResponse> GetGridElecdistributionChartNoArgsYears(string id)
        {
            var result = new Response<GridElecdistributionLineChartResponse>();
            try
            {
                result.Result = _app.GetGridElecdistributionAmountLineChartYear(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        [HttpPost]
        public Response<PageData> GetList(PageReq req)
        {
            var result = new Response<PageData>();
            try
            {
                PageData pd = new PageData();
                long lcount = 0;
                pd.Data = _pikachuApp.GetList<elec_distribution_meter>(req, ref lcount);
                pd.Total = lcount;
                result.Result = pd;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 根据设ID删除数据,可批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string[]> Delete(string[] ids)
        {
            var result = new Response<string[]>();
            try
            {
                result.Result = ids;

                if (!_pikachuApp.Delete_Mask<elec_distribution_meter>(ids))
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 新建数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Create(elec_distribution_meter obj)
        {
            var result = new Response<string>();
            try
            {
                elec_distribution_meter data = _app.Insert(obj);
                if (data != null)
                {
                    result.Result = data.energy_consumption_id;
                }
                else
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "数据写入失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
    }
}
